<!-- 用户管理 -->
<template>
  <div class="app-container">
    <!-- 用户列表 -->
    <el-col>
      <div v-if="isShowSearchBtn" class="search-container">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="关键字" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="关键字"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item label="创建时间">
            <el-date-picker
              class="!w-[240px]"
              v-model="dateTimeRange"
              type="daterange"
              range-separator="~"
              start-placeholder="开始时间"
              end-placeholder="截止时间"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>

          <el-form-item label="状态" prop="is_hid">
            <el-select
              v-model="queryParams.is_hid"
              placeholder="全部"
              clearable
              class="!w-[100px]"
            >
              <el-option label="启用" value="0" />
              <el-option label="禁用" value="1" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleQuery">
              <i-ep-search />搜索
            </el-button>
            <el-button @click="resetQuery"> <i-ep-refresh />重置 </el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-card shadow="never" class="table-container">
        <template #header>
          <div class="flex justify-between">
            <div>
              <el-button v-hasPerm="['article@add']" type="success">
                <router-link to="/cms/article/create"
                  ><i-ep-plus />新增</router-link
                >
              </el-button>
              <el-button
                v-hasPerm="['article@del']"
                type="danger"
                v-if="removeIds.length > 0"
                @click="handleDelete()"
              >
                <i-ep-delete />删除
              </el-button>
            </div>
            <div>
              <el-button
                class="ml-3"
                @click="handleSearchShowBtn"
                icon="Search"
                circle
              />
              <el-button
                class="ml-3"
                title="刷新"
                @click="handleQuery"
                icon="Refresh"
                circle
              />
            </div>
          </div>
        </template>

        <el-table
          v-loading="loading"
          :data="pageData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            key="id"
            label="ID"
            align="center"
            prop="id"
            width="100"
            sortable="custom"
          />
          <el-table-column label="名称" align="center" prop="title" />
          <el-table-column label="分类" align="center">
            <template #default="scope">
              <el-tag type="success">
                {{ scope.row.category.title }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="作者" align="center" prop="author" />
          <el-table-column label="排序" align="center" prop="sort" />
          <el-table-column label="状态" align="center" prop="is_hid">
            <template #default="scope">
              <el-tag
                effect="dark"
                :type="scope.row.is_hid == 0 ? 'success' : 'danger'"
              >
                {{ scope.row.is_hid == 0 ? "启用" : "禁用" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="created_at" />
          <el-table-column label="操作" fixed="right" width="220">
            <template #default="scope">
              <el-button
                v-hasPerm="['article@edit']"
                type="success"
                circle
                title="编辑"
              >
                <router-link :to="addUrl + scope.row.id">
                  <i-ep-edit />
                </router-link>
              </el-button>
              <el-button
                v-hasPerm="['article@del']"
                circle
                type="danger"
                title="删除"
                @click="handleDelete(scope.row.id)"
              >
                <i-ep-delete />
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.p"
          v-model:limit="queryParams.limit"
          @pagination="handleQuery"
        />
      </el-card>
    </el-col>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "MemberLabel",
  inheritAttrs: false,
});

import { getArticlePage, deleteArticle } from "@/api/article";
import { ArticleQuery, ArticlePage } from "@/api/article/types";

const queryFormRef = ref(ElForm); // 查询表单
const isShowSearchBtn = ref(false);
const loading = ref(false); //  加载状态
const removeIds = ref([]); // 删除ID集合 用于批量删除
const addUrl = ref("/cms/article/create?id=");
const queryParams = reactive<ArticleQuery>({
  p: 1,
  limit: 10,
});
const total = ref(0); // 数据总数
const pageData = ref<ArticlePage[]>(); // 分页数据
const dateTimeRange = ref([]);

watch(dateTimeRange, (newVal) => {
  Object.assign(queryParams, { time: newVal });
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getArticlePage(queryParams)
    .then(({ data }) => {
      pageData.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.p = 1;
  dateTimeRange.value = [];
  queryParams.time = undefined;
  handleQuery();
}

/** 行选中 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 删除用户 */
function handleDelete(id?: number) {
  const ids = id || removeIds.value;
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    deleteArticle({ ids: ids }).then(() => {
      handleQuery();
      ElMessage.success("删除成功");
      resetQuery();
    });
  });
}

//收起展开搜索按钮
function handleSearchShowBtn() {
  isShowSearchBtn.value = isShowSearchBtn.value === false;
}

onMounted(() => {
  handleQuery();
});
</script>
